package com.jhhc.Weathers.datasupplement.util;

import com.jhhc.Weathers.datasupplement.entity.BdmsWfProductF;
import com.jhhc.Weathers.datasupplement.entity.zksgBO.WindField;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Component
public class Output {

    public static void dataC(String str1,String str2,String str3) throws IOException {
        File file2 = new File("C:\\Users\\LYQ\\Desktop\\气象文件.csv");
        FileOutputStream fos = new FileOutputStream(file2,true);
        str1 = Double.parseDouble(str1) >= 0 ? "    " + str1:"   " + str1;
        str2 = Double.parseDouble(str2) >= 0 ? "    " + str2:"   " + str2;
        str3 = Double.parseDouble(str3) < 1000 ? "    " + str3 + "0":"    " + str3;
//        if (Double.parseDouble(str1) >= 0) {
//            str1 = "    " + str1;
//        }else {
//            str1 = "   " + str1;
//        }
//        if (Double.parseDouble(str2) >= 0) {
//            str2 = "    " + str2;
//        }else {
//            str2 = "   " + str2;
//        }
//        if (Double.parseDouble(str3) < 1000) {
//            str3 = "    " + str3 + "0";
//        }else {
//            str3 = "    " + str3;
//        }
        //String stnm = "\r\n" + str1 + str2 + str3;
        byte[] sBytes = ("\r\n" + str1 + str2 + str3).getBytes();
        fos.write(sBytes);
        fos.flush();
        fos.close();
    }
    public static void dataCList(List<String> str1,List<String> str2,List<String> str3) throws IOException {
        File file2 = new File("C:\\Users\\LYQ\\Desktop\\气象文件.csv");
        FileOutputStream fos = new FileOutputStream(file2,true);
        String s1 = "";
        String s2 = "";
        String s3 = "";
        for (int i = 0; i < str1.size(); i++) {
            s1 = Double.parseDouble(s1) >= 0 ? "    " + str1:"   " + str1;
            s2 = Double.parseDouble(s2) >= 0 ? "    " + str2:"   " + str2;
            s3 = Double.parseDouble(s3) < 1000 ? "    " + str3 + "0":"    " + str3;
        }

//        if (Double.parseDouble(str1) >= 0) {
//            str1 = "    " + str1;
//        }else {
//            str1 = "   " + str1;
//        }
//        if (Double.parseDouble(str2) >= 0) {
//            str2 = "    " + str2;
//        }else {
//            str2 = "   " + str2;
//        }
//        if (Double.parseDouble(str3) < 1000) {
//            str3 = "    " + str3 + "0";
//        }else {
//            str3 = "    " + str3;
//        }
        //String stnm = "\r\n" + str1 + str2 + str3;
        byte[] sBytes = ("\r\n" + str1 + str2 + str3).getBytes();
        fos.write(sBytes);
        fos.flush();
        fos.close();
    }
    public static void dataTime(String time,String path) throws IOException {
        File file2 = new File(path);
        FileOutputStream fos = new FileOutputStream(file2,true);
        time = "\r\n" + time;
        byte[] sBytes = time.getBytes();
        //System.out.println(time);
        fos.write(sBytes);
        fos.flush();
        fos.close();
    }
    public static void dataInfo(String path,String startTime,String endTime) throws IOException {
        File file2 = new File(path);
        FileOutputStream fos = new FileOutputStream(file2,true);
        String s = "spatial resolution:0.1" + "   startTime:" + startTime + "   endTime:" + endTime;
        byte[] sBytes = s.getBytes();
        fos.write(sBytes);
        fos.flush();
        fos.close();
    }

    public static void dataList(List<WindField> windFields, String path) {
        try {
            File file2 = new File(path);
            FileOutputStream fos = new FileOutputStream(file2,true);
//            String str1; //u
//            String str2; //v
//            String str3; //s
            String startTime = windFields.get(0).getRtm();
            String endTime = windFields.get(windFields.size() - 1).getRtm();
            String s = "spatial resolution:0.1" + "   startTime:" + startTime + "   endTime:" + endTime;
            byte[] sBytes = s.getBytes();
            fos.write(sBytes);
            for (WindField windField : windFields) {
                sBytes = ("\r\n" + windField.getRtm()).getBytes();
                fos.write(sBytes);
                for (int j = 0; j < windField.getS() .size(); j++) {
                    String str1 = Double.parseDouble(windField.getV().get(j)) >= 0 ? "    " + windField.getV().get(j) : "   " + windField.getV().get(j);
                    String str2 = Double.parseDouble(windField.getU().get(j)) >= 0 ? "    " + windField.getU().get(j) : "   " + windField.getU().get(j);
                    String str3 = Double.parseDouble(windField.getS().get(j)) < 1000 ? "    " + windField.getS().get(j) + "0" : "    " + windField.getS().get(j);
                    sBytes = ("\r\n" + str1 + str2 + str3).getBytes();
                    //sBytes = ("\r\n" + str3).getBytes();
                    fos.write(sBytes);
                }
            }
            fos.close();
        }catch (IOException e){
            System.out.println("文件写入异常");
            e.printStackTrace();
        }
    }
    public static void data1(List<WindField> windFields,String path) {
        try {
            File file2 = new File(path);
            FileOutputStream fos = new FileOutputStream(file2,true);
            StringBuilder str1 = new StringBuilder("\r\n"); //u
            StringBuilder str2 = new StringBuilder("\r\n"); //v
            StringBuilder str3 = new StringBuilder("\r\n"); //s
            String startTime = windFields.get(0).getRtm();
            String endTime = windFields.get(windFields.size() - 1).getRtm();
            String s = "spatial resolution:0.1" + "   startTime:" + startTime + "   endTime:" + endTime;
            byte[] sBytes = s.getBytes();
            fos.write(sBytes);
            for (WindField windField : windFields) {
                System.out.println(windField.getRtm());
                sBytes = ("\r\n" + windField.getRtm()).getBytes();
                fos.write(sBytes);
                for (int j = 0; j < windField.getU().size(); j++) {
                    str1.append(windField.getV().get(j)).append(";");
                    str2.append(windField.getU().get(j)).append(";");
                    str3.append(windField.getS().get(j)).append(";");
                }
                for (int i = 0; i < 3; i++) {
                    if (i == 0) {
                        sBytes = str1.toString().getBytes();
                    }else if (i == 1) {
                        sBytes = str2.toString().getBytes();
                    }else {
                        sBytes = str3.toString().getBytes();
                    }
                    fos.write(sBytes);
                }
                str1 = new StringBuilder("\r\n"); //u
                str2 = new StringBuilder("\r\n"); //v
                str3 = new StringBuilder("\r\n"); //s
            }
            fos.flush();
            fos.close();
        }catch (IOException e){
            System.out.println("文件写入异常");
            e.printStackTrace();
        }
    }
    public static void writeGrid(List<BdmsWfProductF> bdmsWfProductFList, String path) throws IOException {
        File file2 = new File(path);
        FileOutputStream fos = new FileOutputStream(file2,true);
        byte[] sBytes = null;
        for (BdmsWfProductF bdmsWfProductF : bdmsWfProductFList) {
            String stnm = "\r\n" + bdmsWfProductF.getGridX() + "," + bdmsWfProductF.getGridY();
            sBytes = stnm.getBytes();
            fos.write(sBytes);
        }
        fos.flush();
        fos.close();
    }
    public static List<String> ybTimes(String hour){
        DecimalFormat format = new DecimalFormat("00");
        List<String> times = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH) + 1;
        int d = calendar.get(Calendar.DATE);
        times.add(year + format.format(m) + format.format(d) + hour);

        calendar.add(Calendar.DATE,-1);
        m = calendar.get(Calendar.MONTH) + 1;
        d = calendar.get(Calendar.DATE);
        times.add(year + format.format(m) + format.format(d) + hour);

//        calendar.add(Calendar.DATE,-2);
//        m = calendar.get(Calendar.MONTH) + 1;
//        d = calendar.get(Calendar.DATE);
//        times.add(year + format.format(m) + format.format(d) + hour);
//
//        calendar.add(Calendar.DATE,-3);
//        m = calendar.get(Calendar.MONTH) + 1;
//        d = calendar.get(Calendar.DATE);
//        times.add(year + format.format(m) + format.format(d) + hour);

        return times;
    }
    /**
     * 时间构造
     * @param ybtime 预报时间yyyyMMddHH0000格式 20230711080000
     * @return yyyy-MM-dd HH:mm:ss格式时间 2023-07-11 08:00:00
     */
    public static String timeConstruction(String ybtime){
        String year = ybtime.substring(0,4);
        String month = ybtime.substring(4,6);
        String day = ybtime.substring(6,8);
        String hour = ybtime.substring(8,10);
        return year + "-" + month + "-" + day + " " + hour + ":00:00";
    }
}
